home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 May / Chip Mayıs 2001.iso / prog / share / 30 / setup.exe / data1.cab / Fsi_-_English / SiteWizard / treelib.js < prev    next >
Encoding:
JavaScript  |  2001-03-15  |  12.8 KB  |  525 lines

  1. //****************************************************************
  2. // You are free to copy the "Folder-Tree" script as long as you
  3. // keep this copyright notice:
  4. // Script found in: http://www.geocities.com/Paris/LeftBank/2178/
  5. // Author: Marcelino Alves Martins (martins@hks.com) December '97.
  6. //****************************************************************
  7.  
  8. //Log of changes:
  9. //       17 Feb 98 - Fix initialization flashing problem with Netscape
  10. //
  11. //       27 Jan 98 - Root folder starts open; support for USETEXTLINKS;
  12. //                   make the ftien4 a js file
  13. //
  14.  
  15.  
  16. // Definition of class Folder
  17. // *****************************************************************
  18.  
  19. function Folder(folderDescription, hreference) //constructor
  20. {
  21.   //constant data
  22.   this.desc = folderDescription
  23.   this.hreference = hreference
  24.   this.id = -1
  25.   this.navObj = 0
  26.   this.iconImg = 0
  27.   this.nodeImg = 0
  28.   this.isLastNode = 0
  29.  
  30.   //dynamic data
  31.   this.isOpen = true
  32.   this.iconSrc = "treelib_page.gif"
  33.   this.children = new Array
  34.   this.nChildren = 0
  35.  
  36.   //methods
  37.   this.initialize = initializeFolder
  38.   this.setState = setStateFolder
  39.   this.addChild = addChild
  40.   this.createIndex = createEntryIndex
  41.   this.hide = hideFolder
  42.   this.display = display
  43.   this.renderOb = drawFolder
  44.   this.totalHeight = totalHeight
  45.   this.subEntries = folderSubEntries
  46.   this.outputLink = outputFolderLink
  47. }
  48.  
  49. function setStateFolder(isOpen)
  50. {
  51.   var subEntries
  52.   var totalHeight
  53.   var fIt = 0
  54.   var i=0
  55.  
  56.   if (isOpen == this.isOpen)
  57.     return
  58.  
  59.   if (browserVersion == 2)
  60.   {
  61.     totalHeight = 0
  62.     for (i=0; i < this.nChildren; i++)
  63.       totalHeight = totalHeight + this.children[i].navObj.clip.height
  64.       subEntries = this.subEntries()
  65.     if (this.isOpen)
  66.       totalHeight = 0 - totalHeight
  67.     for (fIt = this.id + subEntries + 1; fIt < nEntries; fIt++)
  68.       indexOfEntries[fIt].navObj.moveBy(0, totalHeight)
  69.   }
  70.   this.isOpen = isOpen
  71.   propagateChangesInState(this)
  72. }
  73.  
  74. function propagateChangesInState(folder)
  75. {
  76.   var i=0
  77.  
  78.   if (folder.isOpen)
  79.   {
  80.     if (folder.nodeImg)
  81.       if (folder.isLastNode)
  82.         folder.nodeImg.src = "ftv2lastnode.gif"
  83.       else
  84.       folder.nodeImg.src = "ftv2node.gif"
  85.     folder.iconImg.src = "treelib_page.gif"
  86.     for (i=0; i<folder.nChildren; i++)
  87.       folder.children[i].display()
  88.   }
  89.   else
  90.   {
  91.     if (folder.nodeImg)
  92.       if (folder.isLastNode)
  93.         folder.nodeImg.src = "ftv2lastnode.gif"
  94.       else
  95.       folder.nodeImg.src = "ftv2node.gif"
  96.     folder.iconImg.src = "treelib_page.gif"
  97.     for (i=0; i<folder.nChildren; i++)
  98.       folder.children[i].hide()
  99.   }
  100. }
  101.  
  102. function hideFolder()
  103. {
  104.   if (browserVersion == 1) {
  105.     if (this.navObj.style.display == "none")
  106.       return
  107.     this.navObj.style.display = "none"
  108.   } else {
  109.     if (this.navObj.visibility == "hiden")
  110.       return
  111.     this.navObj.visibility = "hiden"
  112.   }
  113.  
  114.   this.setState(0)
  115. }
  116.  
  117. function initializeFolder(level, lastNode, leftSide)
  118. {
  119. var j=0
  120. var i=0
  121. var numberOfFolders
  122. var numberOfDocs
  123. var nc
  124.  
  125.   nc = this.nChildren
  126.  
  127.   this.createIndex()
  128.  
  129.   var auxEv = ""
  130.  
  131.   if (browserVersion > 0)
  132.     auxEv = "<a href='javascript:clickOnNode("+this.id+")'>"
  133.   else
  134.     auxEv = "<a>"
  135.  
  136.   if (level>0)
  137.     if (lastNode) //the last 'brother' in the children array
  138.     {
  139.       this.renderOb(leftSide + "<img name='nodeIcon" + this.id + "' src='ftv2lastnode.gif' width=16 height=22 border=0>")
  140.       leftSide = leftSide + "<img src='ftv2blank.gif' width=16 height=22>"
  141.       this.isLastNode = 1
  142.     }
  143.     else
  144.     {
  145.       this.renderOb(leftSide + "<img name='nodeIcon" + this.id + "' src='ftv2node.gif' width=16 height=22 border=0>")
  146.       leftSide = leftSide + "<img src='ftv2vertline.gif' width=16 height=22>"
  147.       this.isLastNode = 0
  148.     }
  149.   else
  150.     this.renderOb("")
  151.  
  152.   if (nc > 0)
  153.   {
  154.     level = level + 1
  155.     for (i=0 ; i < this.nChildren; i++)
  156.     {
  157.       if (i == this.nChildren-1)
  158.         this.children[i].initialize(level, 1, leftSide)
  159.       else
  160.         this.children[i].initialize(level, 0, leftSide)
  161.       }
  162.   }
  163. }
  164.  
  165. function drawFolder(leftSide)
  166. {
  167.   if (browserVersion == 2) {
  168.     if (!doc.yPos)
  169.       doc.yPos=8
  170.     doc.write("<layer id='folder" + this.id + "' top=" + doc.yPos + " visibility=hiden>")
  171.   }
  172.  
  173.   doc.write("<table ")
  174.   if (browserVersion == 1)
  175.     doc.write(" id='folder" + this.id + "' style='position:block;' ")
  176.   doc.write(" border=0 cellspacing=0 cellpadding=0>")
  177.   doc.write("<tr><td>")
  178.   doc.write(leftSide)
  179.   this.outputLink()
  180.   doc.write("<img name='folderIcon" + this.id + "' ")
  181.   doc.write("src='" + this.iconSrc+"' border=0></a>")
  182.   doc.write("</td><td valign=middle nowrap>")
  183.  
  184.   if (USETEXTLINKS)
  185.   {
  186.     this.outputLink()
  187.  
  188. // ************* changes made for the specs ********************
  189. // The class of the CSS has been disabled and so it uses the 
  190. // styles 'A:link' & 'A:Active' mentioned in the 'global.css'
  191. // The below commented line has been changed
  192. // doc.write("<span class=pagenames>"+ this.desc + "</span></a>")
  193.  
  194.     doc.write("<span>"+ this.desc + "</span></a>")
  195.   }
  196.   else
  197.     doc.write(this.desc)
  198.   doc.write("</td>")
  199.   doc.write("</table>")
  200.  
  201.   if (browserVersion == 2) {
  202.     doc.write("</layer>")
  203.   }
  204.  
  205.   if (browserVersion == 1) {
  206.     this.navObj = doc.all["folder"+this.id]
  207.     this.iconImg = doc.all["folderIcon"+this.id]
  208.     this.nodeImg = doc.all["nodeIcon"+this.id]
  209.   } else if (browserVersion == 2) {
  210.     this.navObj = doc.layers["folder"+this.id]
  211.     this.iconImg = this.navObj.document.images["folderIcon"+this.id]
  212.     this.nodeImg = this.navObj.document.images["nodeIcon"+this.id]
  213.     doc.yPos=doc.yPos+this.navObj.clip.height
  214.   }
  215. }
  216.  
  217. function outputFolderLink()
  218. {
  219.   if (this.hreference)
  220.   {
  221.     doc.write("<a href='" + this.hreference + "' TARGET=\"basefrm\" ")
  222.     if (browserVersion > 0)
  223.       doc.write("onClick='javascript:clickOnFolder("+this.id+")'")
  224.     doc.write(">")
  225.   }
  226.   else
  227.     doc.write("<a>")
  228. //  doc.write("<a href='javascript:clickOnFolder("+this.id+")'>")
  229. }
  230.  
  231. function addChild(childNode)
  232. {
  233.   this.children[this.nChildren] = childNode
  234.   this.nChildren++
  235.   return childNode
  236. }
  237.  
  238. function folderSubEntries()
  239. {
  240.   var i = 0
  241.   var se = this.nChildren
  242.  
  243.   for (i=0; i < this.nChildren; i++){
  244.     if (this.children[i].children) //is a folder
  245.       se = se + this.children[i].subEntries()
  246.   }
  247.  
  248.   return se
  249. }
  250.  
  251.  
  252. // Definition of class Item (a document or link inside a Folder)
  253. // *************************************************************
  254.  
  255. function Item(itemDescription, itemLink) // Constructor
  256. {
  257.   // constant data
  258.   this.desc = itemDescription
  259.   this.link = itemLink
  260.   this.id = -1 //initialized in initalize()
  261.   this.navObj = 0 //initialized in render()
  262.   this.iconImg = 0 //initialized in render()
  263.   this.iconSrc = "treelib_page.gif"
  264.  
  265.   // methods
  266.   this.initialize = initializeItem
  267.   this.createIndex = createEntryIndex
  268.   this.hide = hideItem
  269.   this.display = display
  270.   this.renderOb = drawItem
  271.   this.totalHeight = totalHeight
  272. }
  273.  
  274. function hideItem()
  275. {
  276.   if (browserVersion == 1) {
  277.     if (this.navObj.style.display == "none")
  278.       return
  279.     this.navObj.style.display = "none"
  280.   } else {
  281.     if (this.navObj.visibility == "hiden")
  282.       return
  283.     this.navObj.visibility = "hiden"
  284.   }
  285. }
  286.  
  287. function initializeItem(level, lastNode, leftSide)
  288. {
  289.   this.createIndex()
  290.  
  291.   if (level>0)
  292.     if (lastNode) //the last 'brother' in the children array
  293.     {
  294.       this.renderOb(leftSide + "<img src='ftv2lastnode.gif' width=16 height=22>")
  295.       leftSide = leftSide + "<img src='ftv2blank.gif' width=16 height=22>"
  296.     }
  297.     else
  298.     {
  299.       this.renderOb(leftSide + "<img src='ftv2node.gif' width=16 height=22>")
  300.       leftSide = leftSide + "<img src='ftv2vertline.gif' width=16 height=22>"
  301.     }
  302.   else
  303.     this.renderOb("")
  304. }
  305.  
  306. function drawItem(leftSide)
  307. {
  308.   if (browserVersion == 2)
  309.     doc.write("<layer id='item" + this.id + "' top=" + doc.yPos + " visibility=hiden>")
  310.  
  311.   doc.write("<table ")
  312.   if (browserVersion == 1)
  313.     doc.write(" id='item" + this.id + "' style='position:block;' ")
  314.   doc.write(" border=0 cellspacing=0 cellpadding=0>")
  315.   doc.write("<tr><td>")
  316.   doc.write(leftSide)
  317.   doc.write("<a href=" + this.link + ">")
  318.   doc.write("<img id='itemIcon"+this.id+"' ")
  319.   doc.write("src='"+this.iconSrc+"' border=0>")
  320.   doc.write("</a>")
  321.   doc.write("</td><td valign=middle nowrap>")
  322.   if (USETEXTLINKS)
  323.     doc.write("<a href=" + this.link + ">" +this.desc + "</a>")
  324.   else
  325.     doc.write(this.desc)
  326.   doc.write("</table>")
  327.  
  328.   if (browserVersion == 2)
  329.     doc.write("</layer>")
  330.  
  331.   if (browserVersion == 1) {
  332.     this.navObj = doc.all["item"+this.id]
  333.     this.iconImg = doc.all["itemIcon"+this.id]
  334.   } else if (browserVersion == 2) {
  335.     this.navObj = doc.layers["item"+this.id]
  336.     this.iconImg = this.navObj.document.images["itemIcon"+this.id]
  337.     doc.yPos=doc.yPos+this.navObj.clip.height
  338.   }
  339. }
  340.  
  341.  
  342. // Methods common to both objects (pseudo-inheritance)
  343. // ********************************************************
  344.  
  345. function display()
  346. {
  347.   if (browserVersion == 1)
  348.     this.navObj.style.display = "block"
  349.   else
  350.     this.navObj.visibility = "show"
  351. }
  352.  
  353. function createEntryIndex()
  354. {
  355.   this.id = nEntries
  356.   indexOfEntries[nEntries] = this
  357.   nEntries++
  358. }
  359.  
  360. // total height of subEntries open
  361. function totalHeight() //used with browserVersion == 2
  362. {
  363.   var h = this.navObj.clip.height
  364.   var i = 0
  365.  
  366.   if (this.isOpen) //is a folder and _is_ open
  367.     for (i=0 ; i < this.nChildren; i++)
  368.       h = h + this.children[i].totalHeight()
  369.  
  370.   return h
  371. }
  372.  
  373.  
  374. // Events
  375. // *********************************************************
  376.  
  377. function clickOnFolder(folderId)
  378. {
  379.   var clicked = indexOfEntries[folderId]
  380.  
  381.   // rjs: add call to change icon
  382.   selectClickedFolder(folderId)
  383.  
  384.   // gsk: update the name of the page in the structure frame.
  385.   document.pagename.currentpagename.value = getFolderDescription(folderId);
  386.  
  387.   if (!clicked.isOpen)
  388.     clickOnNode(folderId)
  389.  
  390.   return
  391.  
  392.   if (clicked.isSelected)
  393.     return
  394. }
  395.  
  396. function clickOnNode(folderId)
  397. {
  398.   var clickedFolder = 0
  399.   var state = 0
  400.  
  401.   clickedFolder = indexOfEntries[folderId]
  402.   state = clickedFolder.isOpen
  403.  
  404.   // rjs: add call to change icon
  405.   selectClickedFolder(folderId)
  406.  
  407.   clickedFolder.setState(!state) //open<->close
  408. }
  409.  
  410. function initializeDocument(current)
  411. {
  412.   if (doc.all)
  413.     browserVersion = 1 //IE4
  414.   else
  415.     if (doc.layers)
  416.       browserVersion = 2 //NS4
  417.     else
  418.       browserVersion = 0 //other
  419.  
  420.   foldersTree.initialize(0, 1, "")
  421.   foldersTree.display()
  422.  
  423.   if (browserVersion > 0)
  424.   {
  425.     doc.write("<layer top="+indexOfEntries[nEntries-1].navObj.top+"> </layer>")
  426.   }
  427.  
  428.   if (browserVersion != 1) // If not IE, click on nodes to make tree appear
  429.   {
  430.     // close the whole tree
  431.     clickOnNode(0)
  432.     // open the root folder
  433.     clickOnNode(0)
  434.   }
  435.     // open the root folder AA 5/3/00
  436.     for (i=0 ; i < nEntries; i++)
  437.     {
  438.       var clickedFolder = 0
  439.       var state = 0
  440.  
  441.       clickedFolder = indexOfEntries[i]
  442.       clickedFolder.setState(false)
  443.       selectClickedFolder(i)
  444.       clickOnNode(i)
  445.     }
  446.   document.pagename.rootpagename.value = getFolderDescription(0);
  447.   if (current != -1)
  448.     clickOnFolder(current);
  449. }
  450.  
  451. // Auxiliary Functions for Folder-Treee backward compatibility
  452. // *********************************************************
  453.  
  454. function gFld(description, hreference)
  455. {
  456.   folder = new Folder(description, hreference)
  457.   return folder
  458. }
  459.  
  460. function gLnk(target, description, linkData)
  461. {
  462.   fullLink = ""
  463.  
  464.   if (target==0)
  465.   {
  466.     fullLink = "'"+linkData+"' target=\"basefrm\""
  467.   }
  468.   else
  469.   {
  470.     if (target==1)
  471.        fullLink = "'http://"+linkData+"' target=_blank"
  472.     else
  473.        fullLink = "'http://"+linkData+"' target=\"basefrm\""
  474.   }
  475.  
  476.   linkItem = new Item(description, fullLink)
  477.   return linkItem
  478. }
  479.  
  480. function insFld(parentFolder, childFolder)
  481. {
  482.   return parentFolder.addChild(childFolder)
  483. }
  484.  
  485. function insDoc(parentFolder, document)
  486. {
  487.   parentFolder.addChild(document)
  488. }
  489.  
  490. // Functions added to maintain highlighted icon; added by rjs
  491. // ********************************************
  492.  
  493. function selectClickedFolder(folderId)
  494. {
  495.   var clicked = indexOfEntries[folderId]
  496.   var lastUsed;
  497.  
  498.   if (lastClickedFolder != -1)
  499.   {
  500.     lastUsed = indexOfEntries[lastClickedFolder]
  501.     lastUsed.iconImg.src="treelib_page.gif"
  502.   }
  503.  
  504.   clicked.iconImg.src="treelib_page_selected.gif"
  505.   lastClickedFolder = folderId
  506. }
  507.  
  508. // gsk: to access the foldername of the currently clicked node
  509. function getFolderDescription(folderId)
  510. {
  511.   var clicked = indexOfEntries[folderId];
  512.   return clicked.desc;
  513. }
  514.  
  515. // Global variables
  516. // ****************
  517.  
  518. USETEXTLINKS = 1
  519. indexOfEntries = new Array
  520. nEntries = 0
  521. doc = document
  522. browserVersion = 0
  523. selectedFolder=0
  524. lastClickedFolder=-1
  525.